package cn.wx.myview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by wx on 2020/7/27.
 */
public class MyView extends View {

    Paint paint;
    int left;
    int top;
    int right;
    int bottom;

    float x_down = 0;
    float y_down = 0;
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;
    float oldRotation = 0;
    Matrix matrix = new Matrix();
    Matrix matrix1 = new Matrix();
    Matrix savedMatrix = new Matrix();

    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    int mode = NONE;

    boolean matrixCheck = false;

    int widthScreen;
    int heightScreen;

    Context context;

    float scale = 0.5f;

    private Canvas canvas;

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    void init(){
        paint = new Paint();
        paint.setColor(Color.RED);
//        DisplayMetrics dm = new DisplayMetrics();
//        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
//        widthScreen = dm.widthPixels;
//        heightScreen = dm.heightPixels;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        this.top = top;
        this.left = left;
        this.right = right;
        this.bottom = bottom;
        widthScreen = right;
        heightScreen = bottom;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.drawColor(Color.GREEN);

        canvas.setMatrix(matrix);
        Rect rect = new Rect(10,10,100,200);
        canvas.drawRect(rect,paint);

        canvas.drawCircle(right/2,bottom/2,50,paint);
//        canvas.translate(right/2,bottom/2);
//        canvas.rotate(90);
        canvas.restore();


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                //第一个手指初次接触屏幕时触发

                mode = DRAG;
                x_down = event.getX();
                y_down = event.getY();
                Log.e("按下x",x_down+"");
                Log.e("按下y",y_down+"");
                savedMatrix.set(matrix);

                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                // 有非主要的手指按下(即按下之前已经有手指在屏幕上)
                mode = ZOOM;
                oldDist = spacing(event);
                oldRotation = rotation(event);
                savedMatrix.set(matrix);
                midPoint(mid, event);
                break;
            case MotionEvent.ACTION_MOVE:
                //手指在屏幕滑动时触发
                if (mode == ZOOM) {
                    matrix1.set(savedMatrix);
                    float rotation = rotation(event) - oldRotation;
                    float newDist = spacing(event);
                    scale = newDist / oldDist;
                    Log.e("缩放",scale+"**");
                    matrix1.postScale(scale, scale, mid.x, mid.y);// 縮放
                    matrix1.postRotate(rotation, mid.x, mid.y);// 旋轉
                    matrixCheck = matrixCheck();
//                    if (matrixCheck == false) {
                        matrix.set(matrix1);
                        invalidate();
//                    }
                } else if (mode == DRAG) {
                    matrix1.set(savedMatrix);
                    matrix1.postTranslate(event.getX() - x_down, event.getY()
                            - y_down);// 平移
                    matrixCheck = matrixCheck();
                    matrixCheck = matrixCheck();
//                    if (matrixCheck == false) {
                        matrix.set(matrix1);
                        invalidate();
//                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                // 最后一个手指离开屏幕时触发
            case MotionEvent.ACTION_POINTER_UP:
                // 有非主要的手指抬起(即抬起之后仍然有手指在屏幕上)
                mode = NONE;
                break;
                default:
                    break;
        }

        return true;
    }

    private boolean matrixCheck() {
        float[] f = new float[9];
        matrix1.getValues(f);
        // 图片4个顶点的坐标
        float x1 = f[0] * 0 + f[1] * 0 + f[2];
        float y1 = f[3] * 0 + f[4] * 0 + f[5];
        float x2 = f[0] * this.getWidth() + f[1] * 0 + f[2];
        float y2 = f[3] * this.getWidth() + f[4] * 0 + f[5];
        float x3 = f[0] * 0 + f[1] * this.getHeight() + f[2];
        float y3 = f[3] * 0 + f[4] * this.getHeight() + f[5];
        float x4 = f[0] * this.getWidth() + f[1] * this.getHeight()
                + f[2];
        float y4 = f[3] * this.getWidth() + f[4] * this.getHeight()
                + f[5];
        // 图片现宽�?
        double width = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        // 缩放比率判断
        if (width < widthScreen / 3 || width > widthScreen * 3) {
            return true;
        }
        // 出界判断
        if ((x1 < widthScreen / 3 && x2 < widthScreen / 3
                && x3 < widthScreen / 3 && x4 < widthScreen / 3)
                || (x1 > widthScreen * 2 / 3 && x2 > widthScreen * 2 / 3
                && x3 > widthScreen * 2 / 3 && x4 > widthScreen * 2 / 3)
                || (y1 < heightScreen / 3 && y2 < heightScreen / 3
                && y3 < heightScreen / 3 && y4 < heightScreen / 3)
                || (y1 > heightScreen * 2 / 3 && y2 > heightScreen * 2 / 3
                && y3 > heightScreen * 2 / 3 && y4 > heightScreen * 2 / 3)) {
            return true;
        }
        return false;
    }

    // 触碰两点间距�?
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return  (float)Math.sqrt(x * x + y * y);
    }

    // 取手势中心点
    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }

    // 取旋转角�?
    private float rotation(MotionEvent event) {
        double delta_x = (event.getX(0) - event.getX(1));
        double delta_y = (event.getY(0) - event.getY(1));
        double radians = Math.atan2(delta_y, delta_x);
        Log.e("旋转角度",radians+"******************************");
        return (float) Math.toDegrees(radians);
    }

    /**
     * 坐标换算
     */
    public  void coordinateCalculation(){
        float[] valuess = new float[9];
        matrix.getValues(valuess);

        for (int i = 0 ;i<valuess.length;i++){
            Log.e("前values",valuess[i]+"");
        }
        boolean invert = matrix.invert(matrix);
        Log.e("是否可以逆运算",invert+"");
//        matrix.mapPoints();
//        matrix.getValues();
        float[] values = new float[9];
        matrix.getValues(values);

        for (int i = 0 ;i<values.length;i++){
            Log.e("后values",values[i]+"");
        }

        float[] valuesT = new float[2];
        valuesT[0] = x_down;
        valuesT[1] = y_down;

        matrix.mapPoints(valuesT);
        for (int i = 0 ;i<valuesT.length;i++){
            Log.e("valuesT",valuesT[i]+"");
        }

    } //


    /**
     *  c换算为场景左边  转换为原始矩阵
     * @param x
     * @param y
     * @return
     */
    public PointF transformation(float x,float y){
        boolean invert = matrix.invert(matrix);
        float[] valuesT = new float[2];
        valuesT[0] = x;
        valuesT[1] = y;
        matrix.mapPoints(valuesT);
       return new PointF(valuesT[0],valuesT[1]);
    }

    /**
     * 缩放
     * @param scale
     */
    public void setScale(float scale){
        this.scale = scale;
//        matrix1.set(savedMatrix);
//        matrix1.postScale(this.getWidth(),this.getHeight(), mid.x, mid.y);
//        matrix.set(matrix1);
//        invalidate();
    }






} //
